const ocrUtils = {
  captureImgOrcResult: [],
  captureImg: null,
  capturing: true,
  captureAndOcr() {
    this.capturing = true;
    this.captureImg && this.captureImg.recycle();
    this.captureImg = captureScreen();
    if (!this.captureImg) {
      toastLog("截图失败");
    }
    this.captureImgOrcResult = gmlkit.ocr(this.captureImg, "zh").toArray(3);
    this.capturing = false;
  },
  clickText(waitSec, strs) {
    const regExps = [];
    for (let i = 0; i < strs.length; i++) {
      let str = strs[i];
      let regExp = regExpUtils.getRegExp(str);
      regExps.push(regExp);
    }
    let ocrText = this.existText(waitSec, regExps);
    if (ocrText) {
      toastLog("开始点击： " + regExps.toString() + "");
      click(ocrText.bounds.centerX(), ocrText.bounds.centerY());
      toastLog("点击完成： " + regExps.toString() + "");
      return ocrText;
    }
  },
  click(left, top, right, bottom) {
    return click(left, top, right, bottom);
  },
  existText(waitSec, regExps) {
    waitSec = waitSec || 0;
    toastLog("等待" + waitSec + "秒后，开始查找: " + regExps.toString() + "");
    sleep(waitSec * 1000);
    if (waitSec > 0) {
      this.captureAndOcr();
    }
    for (let i = 0; i < this.captureImgOrcResult.length; i++) {
      let item = this.captureImgOrcResult[i];
      for (let j = 0; j < regExps.length; j++) {
        let regExp = regExps[j];
        if (regExp.test(item.text)) {
          toastLog("找到: " + regExp.toString() + "");
          return item;
        }
      }
    }
    toastLog("没找到: " + regExps.toString() + "");
    ocrLogUtils.append(
      "没找到: " +
        regExps.toString() +
        "" +
        JSON.stringify(this.getOcrTexts()) +
        "\n\n\n"
    );
  },
  getOcrTexts() {
    let texts = [];
    for (let i = 0; i < this.captureImgOrcResult.length; i++) {
      texts.push(this.captureImgOrcResult[i].text);
    }
    return texts;
  },
  openApp(appName) {
    launchApp(appName);
    sleep(2000);
    toastLog("open app success:" + appName);
  },
  swipeX(duration, distance) {
    distance = distance || 200;
    let i = 0;
    let j = 0;
    let flag = duration > 10;
    while (flag) {
      let interval = random(15, 25);
      toastLog("等待" + interval + "秒开始滑动");
      sleep(interval * 1000);
      swipe(1080 / 2, 1000, 1080 / 2, 1000 - distance, 30);
      duration = duration - interval;
      i = i + 1;
      j = j + interval;
      if (duration < 25) {
        flag = false;
      }
      toastLog("滑动完成，已滑动" + i + "次，耗时" + j + "秒");
    }
    if (duration > 0) {
      toastLog("等待" + duration + "秒开始滑动");
      sleep(duration);
      i = i + 1;
      swipe(1080 / 2, 1000, 1080 / 2, 800, 30);
      toastLog("滑动完成，已滑动" + i + "次，耗时" + (j + duration) + "秒");
    }
  },
  sleepAndLog(sec, iv) {
    iv = iv || 10;
    let i = 0;
    while (sec > iv) {
      sleep(iv * 1000);
      i++;
      sec = sec - iv;
      toastLog("已等待" + i * iv + "秒，还需等待" + sec + "秒");
    }
    if (sec > 0) {
      sleep(sec * 1000);
    }
  },
  getStatusBarHeightCompat() {
    let result = 0;
    let resId = context
      .getResources()
      .getIdentifier("status_bar_height", "dimen", "android");
    if (resId > 0) {
      result = context.getResources().getDimensionPixelOffset(resId);
    }
    if (result <= 0) {
      result = context
        .getResources()
        .getDimensionPixelOffset(R.dimen.dimen_25dp);
    }
    return result;
  },
};